home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cctools / as / write_object.c < prev    next >
C/C++ Source or Header  |  1994-06-03  |  37KB  |  1,178 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <sys/file.h>
  4. #include <bsd/libc.h>
  5. #include <mach/mach.h>
  6. #include <mach-o/loader.h>
  7. #include <mach-o/reloc.h>
  8. #ifdef I860
  9. #include <mach-o/i860/reloc.h>
  10. #endif
  11. #ifdef M88K
  12. #include <mach-o/m88k/reloc.h>
  13. #endif
  14. #ifdef M98K
  15. #include <mach-o/m98k/reloc.h>
  16. #endif
  17. #ifdef HPPA
  18. #include <mach-o/hppa/reloc.h>
  19. #include "stuff/hppa.h"
  20. #endif
  21. #ifdef SPARC
  22. #include <mach-o/sparc/reloc.h>
  23. #endif
  24. #include "stuff/round.h"
  25. #include "stuff/bytesex.h"
  26. #include "stuff/errors.h"
  27. #include "as.h"
  28. #include "struc-symbol.h"
  29. #include "symbols.h"
  30. #include "frags.h"
  31. #include "fixes.h"
  32. #include "md.h"
  33. #include "sections.h"
  34. #include "messages.h"
  35. #include "xmalloc.h"
  36. #ifdef I860
  37. #define RELOC_SECTDIFF    I860_RELOC_SECTDIFF
  38. #define RELOC_PAIR    I860_RELOC_PAIR
  39. #endif
  40. #ifdef M88K
  41. #define RELOC_SECTDIFF    M88K_RELOC_SECTDIFF
  42. #define RELOC_PAIR    M88K_RELOC_PAIR
  43. #endif
  44. #ifdef M98K
  45. #define RELOC_SECTDIFF    M98K_RELOC_SECTDIFF
  46. #define RELOC_PAIR    M98K_RELOC_PAIR
  47. #endif
  48. #ifdef HPPA
  49. #define RELOC_SECTDIFF    HPPA_RELOC_SECTDIFF
  50. #define RELOC_PAIR    HPPA_RELOC_PAIR
  51. #endif
  52. #ifdef SPARC
  53. #define RELOC_SECTDIFF    SPARC_RELOC_SECTDIFF
  54. #define RELOC_PAIR    SPARC_RELOC_PAIR
  55. #endif
  56. #if defined(M68K) || defined(I386)
  57. #define RELOC_SECTDIFF    GENERIC_RELOC_SECTDIFF
  58. #define RELOC_PAIR    GENERIC_RELOC_PAIR
  59. #endif
  60.  
  61. /*
  62.  * These variables are set by layout_symbols() to organize the symbol table and
  63.  * string table in order the dynamic linker expects.  They are then used in
  64.  * write_object() to put out the symbols and strings in that order.
  65.  * The order of the symbol table is:
  66.  *    local symbols
  67.  *    defined external symbols (sorted by name)
  68.  *    undefined external symbols (sorted by name)
  69.  * The order of the string table is:
  70.  *    strings for external symbols
  71.  *    strings for local symbols
  72.  */
  73. /* index to and number of local symbols */
  74. static unsigned long ilocalsym = 0;
  75. static unsigned long nlocalsym = 0;
  76. /* index to, number of and array of sorted externally defined symbols */
  77. static unsigned long iextdefsym = 0;
  78. static unsigned long nextdefsym = 0;
  79. static symbolS **extdefsyms = NULL;
  80. /* index to, number of and array of sorted undefined symbols */
  81. static unsigned long iundefsym = 0;
  82. static unsigned long nundefsym = 0;
  83. static symbolS **undefsyms = NULL;
  84.  
  85. static unsigned long layout_indirect_symbols(
  86.      void);
  87. static void layout_symbols(
  88.     long *symbol_number,
  89.     long *string_byte_count);
  90. static int qsort_compare(
  91.     const symbolS **sym1,
  92.     const symbolS **sym2);
  93. static unsigned long nrelocs_for_fix(
  94.     struct fix *fixP);
  95. static unsigned long fix_to_relocation_entries(
  96.     struct fix *fixP,
  97.     unsigned long sect_addr,
  98.     struct relocation_info *riP);
  99. #ifdef I860
  100. static void
  101.     I860_tweeks(void);
  102. #endif
  103.  
  104. /*
  105.  * write_object() writes a Mach-O object file from the built up data structures.
  106.  */
  107. void
  108. write_object(
  109. char *out_file_name)
  110. {
  111.     /* The structures for Mach-O relocatables */
  112.     struct mach_header        header;
  113.     struct segment_command    reloc_segment;
  114.     struct symtab_command    symbol_table;
  115.     struct dysymtab_command    dynamic_symbol_table;
  116.     unsigned long        section_type, *indirect_symbols;
  117.     isymbolS            *isymbolP;
  118.     unsigned long        i, j, nsects, nsyms, strsize, nindirectsyms;
  119.  
  120.     /* locals to fill in section struct fields */
  121.     unsigned long offset, zero;
  122.  
  123.     /* The GAS data structures */
  124.     struct frchain *frchainP, *p;
  125.     struct symbol *symbolP;
  126.     struct frag *fragP;
  127.     struct fix *fixP;
  128.  
  129.     unsigned long output_size;
  130.     char *output_addr;
  131.     kern_return_t r;
  132.  
  133.     enum byte_sex host_byte_sex;
  134.     unsigned long reloff, nrelocs;
  135.     long count;
  136.     char *fill_literal;
  137.     long fill_size;
  138.     char *symbol_name;
  139.     int fd;
  140.  
  141. #ifdef I860
  142.     I860_tweeks();
  143. #endif
  144.     i = 0; /* to shut up a compiler "may be used uninitialized" warning */
  145.  
  146.     /*
  147.      * The first group of things to do is to set all the fields in the
  148.      * header structures which includes offsets and determining the final
  149.      * sizes of things.
  150.      */
  151.  
  152.     /* 
  153.      * Fill in the addr and size fields of each section structure and count
  154.      * the number of sections.
  155.      */
  156.     nsects = 0;
  157.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  158.         frchainP->frch_section.addr = frchainP->frch_root->fr_address;
  159.         frchainP->frch_section.size = frchainP->frch_last->fr_address -
  160.                          frchainP->frch_root->fr_address;
  161.         nsects++;
  162.     }
  163.  
  164.     /*
  165.      * Setup the indirect symbol tables by looking up or creating symbol
  166.      * from the indirect symbol names and recording the symbol pointers.
  167.      */
  168.     nindirectsyms = layout_indirect_symbols();
  169.  
  170.     /*
  171.      * Setup the symbol table to include only those symbols that will be in
  172.      * the object file, assign the string table offsets into the symbols
  173.      * and size the string table.
  174.      */
  175.     nsyms = 0;
  176.     strsize = 0;
  177.     layout_symbols(&nsyms, &strsize);
  178.  
  179.     /* fill in the Mach-O header */
  180.     header.magic = MH_MAGIC;
  181.     header.cputype = md_cputype;
  182.     if(archflag_cpusubtype != -1)
  183.         header.cpusubtype = archflag_cpusubtype;
  184.     else
  185.         header.cpusubtype = md_cpusubtype;
  186.  
  187.     header.filetype = MH_OBJECT;
  188.     header.ncmds = 0;
  189.     header.sizeofcmds = 0;
  190.     if(nsects != 0){
  191.         header.ncmds += 1;
  192.         header.sizeofcmds += sizeof(struct segment_command) +
  193.                  nsects * sizeof(struct section);
  194.     }
  195.     if(nsyms != 0){
  196.         header.ncmds += 1;
  197.         header.sizeofcmds += sizeof(struct symtab_command);
  198.         if(flagseen['k']){
  199.         header.ncmds += 1;
  200.         header.sizeofcmds += sizeof(struct dysymtab_command);
  201.         }
  202.     }
  203.     else
  204.         strsize = 0;
  205.     header.flags = 0;
  206.  
  207.     /* fill in the segment command */
  208.     memset(&reloc_segment, '\0', sizeof(struct segment_command));
  209.     reloc_segment.cmd = LC_SEGMENT;
  210.     reloc_segment.cmdsize = sizeof(struct segment_command) +
  211.                 nsects * sizeof(struct section);
  212.     /* leave reloc_segment.segname full of zeros */
  213.     reloc_segment.vmaddr = 0;
  214.     reloc_segment.vmsize = 0;
  215.     reloc_segment.filesize = 0;
  216.     offset = header.sizeofcmds + sizeof(struct mach_header);
  217.     reloc_segment.fileoff = offset;
  218.     reloc_segment.maxprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  219.     reloc_segment.initprot= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  220.     reloc_segment.nsects = nsects;
  221.     reloc_segment.flags = 0;
  222.     /*
  223.      * Set the offsets to the contents of the sections (for non-zerofill
  224.      * sections) and set the filesize and vmsize of the segment.  This is
  225.      * complicated by the fact that all the zerofill sections have addresses
  226.      * after the non-zerofill sections and that the alignment of sections
  227.      * produces gaps that are not in any section.  For the vmsize we rely on
  228.      * the fact the the sections start at address 0 so it is just the last
  229.      * zerofill section or the last not-zerofill section.
  230.      */
  231.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  232.         if((frchainP->frch_section.flags & SECTION_TYPE) == S_ZEROFILL)
  233.         continue;
  234.         for(p = frchainP->frch_next; p != NULL; p = p->frch_next)
  235.         if((p->frch_section.flags & SECTION_TYPE) != S_ZEROFILL)
  236.             break;
  237.         if(p != NULL)
  238.         i = p->frch_section.addr - frchainP->frch_section.addr;
  239.         else
  240.         i = frchainP->frch_section.size;
  241.         reloc_segment.filesize += i;
  242.         frchainP->frch_section.offset = offset;
  243.         offset += i;
  244.         reloc_segment.vmsize = frchainP->frch_section.addr +
  245.                    frchainP->frch_section.size;
  246.     }
  247.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  248.         if((frchainP->frch_section.flags & SECTION_TYPE) != S_ZEROFILL)
  249.         continue;
  250.         reloc_segment.vmsize = frchainP->frch_section.addr +
  251.                    frchainP->frch_section.size;
  252.     }
  253.     offset = round(offset, sizeof(long));
  254.  
  255.     /*
  256.      * Count the number of relocation entries for each section.
  257.      */
  258.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  259.         frchainP->frch_section.nreloc = 0;
  260.         for(fixP = frchainP->frch_fix_root; fixP; fixP = fixP->fx_next){
  261.         frchainP->frch_section.nreloc += nrelocs_for_fix(fixP);
  262.         }
  263.     }
  264.  
  265.     /*
  266.      * Fill in the offset to the relocation entries of the sections.
  267.      */
  268.     offset = round(offset, sizeof(long));
  269.     reloff = offset;
  270.     nrelocs = 0;
  271.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  272.         if(frchainP->frch_section.nreloc == 0)
  273.         frchainP->frch_section.reloff = 0;
  274.         else
  275.         frchainP->frch_section.reloff = offset;
  276.         offset += frchainP->frch_section.nreloc *
  277.               sizeof(struct relocation_info);
  278.         nrelocs += frchainP->frch_section.nreloc;
  279.     }
  280.  
  281.     if(flagseen['k']){
  282.         /* fill in the fields of the dysymtab_command */
  283.         dynamic_symbol_table.cmd = LC_DYSYMTAB;
  284.         dynamic_symbol_table.cmdsize = sizeof(struct dysymtab_command);
  285.  
  286.         dynamic_symbol_table.ilocalsym = ilocalsym;
  287.         dynamic_symbol_table.nlocalsym = nlocalsym;
  288.         dynamic_symbol_table.iextdefsym = iextdefsym;
  289.         dynamic_symbol_table.nextdefsym = nextdefsym;
  290.         dynamic_symbol_table.iundefsym = iundefsym;
  291.         dynamic_symbol_table.nundefsym = nundefsym;
  292.  
  293.         if(nindirectsyms == 0){
  294.         dynamic_symbol_table.nindirectsyms = 0;
  295.         dynamic_symbol_table.indirectsymoff = 0;
  296.         }
  297.         else{
  298.         dynamic_symbol_table.nindirectsyms = nindirectsyms;
  299.         dynamic_symbol_table.indirectsymoff = offset;
  300.         offset += nindirectsyms * sizeof(unsigned long);
  301.         }
  302.  
  303.         dynamic_symbol_table.tocoff = 0;
  304.         dynamic_symbol_table.ntoc = 0;
  305.         dynamic_symbol_table.modtaboff = 0;
  306.         dynamic_symbol_table.nmodtab = 0;
  307.         dynamic_symbol_table.extrefsymoff = 0;
  308.         dynamic_symbol_table.nextrefsyms = 0;
  309.         dynamic_symbol_table.extreloff = 0;
  310.         dynamic_symbol_table.nextrel = 0;
  311.         dynamic_symbol_table.locreloff = 0;
  312.         dynamic_symbol_table.nlocrel = 0;
  313.     }
  314.  
  315.     /* fill in the fields of the symtab_command (except the string table) */
  316.     symbol_table.cmd = LC_SYMTAB;
  317.     symbol_table.cmdsize = sizeof(struct symtab_command);
  318.     if(nsyms == 0)
  319.         symbol_table.symoff = 0;
  320.     else
  321.         symbol_table.symoff = offset;
  322.     symbol_table.nsyms = nsyms;
  323.     offset += symbol_table.nsyms * sizeof(struct nlist);
  324.  
  325.     /* fill in the string table fields of the symtab_command */
  326.     if(strsize == 0)
  327.         symbol_table.stroff = 0;
  328.     else
  329.         symbol_table.stroff = offset;
  330.     symbol_table.strsize = round(strsize, sizeof(unsigned long));
  331.     offset += round(strsize, sizeof(unsigned long));
  332.  
  333.     /*
  334.      * The second group of things to do is now with the size of everything
  335.      * known the object file and the offsets set in the various structures
  336.      * the contents of the object file can be created.
  337.      */
  338.  
  339.     /*
  340.      * Create the buffer to copy the parts of the output file into.
  341.      */
  342.     output_size = offset;
  343.     if((r = vm_allocate(task_self(), (vm_address_t *)&output_addr,
  344.                 output_size, TRUE)) != KERN_SUCCESS)
  345.         as_fatal("can't vm_allocate() buffer for output file of size %lu",
  346.              output_size);
  347.  
  348.     /* put the headers in the output file's buffer */
  349.     host_byte_sex = get_host_byte_sex();
  350.     offset = 0;
  351.  
  352.     /* put the mach_header in the buffer */
  353.     memcpy(output_addr + offset, &header, sizeof(struct mach_header));
  354.     if(host_byte_sex != md_target_byte_sex)
  355.         swap_mach_header((struct mach_header *)(output_addr + offset),
  356.                  md_target_byte_sex);
  357.     offset += sizeof(struct mach_header);
  358.  
  359.     /* put the segment_command in the buffer */
  360.     if(nsects != 0){
  361.         memcpy(output_addr + offset, &reloc_segment,
  362.            sizeof(struct segment_command));
  363.         if(host_byte_sex != md_target_byte_sex)
  364.         swap_segment_command((struct segment_command *)
  365.                      (output_addr + offset),
  366.                      md_target_byte_sex);
  367.         offset += sizeof(struct segment_command);
  368.     }
  369.  
  370.     /* put the segment_command's section structures in the buffer */
  371.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  372.         memcpy(output_addr + offset, &(frchainP->frch_section),
  373.            sizeof(struct section));
  374.         if(host_byte_sex != md_target_byte_sex)
  375.         swap_section((struct section *)(output_addr + offset), 1,
  376.                      md_target_byte_sex);
  377.         offset += sizeof(struct section);
  378.     }
  379.  
  380.     /* put the symbol_command in the buffer */
  381.     if(nsyms != 0){
  382.         memcpy(output_addr + offset, &symbol_table,
  383.            sizeof(struct symtab_command));
  384.         if(host_byte_sex != md_target_byte_sex)
  385.         swap_symtab_command((struct symtab_command *)
  386.                      (output_addr + offset),
  387.                      md_target_byte_sex);
  388.         offset += sizeof(struct symtab_command);
  389.     }
  390.  
  391.     if(flagseen['k']){
  392.         /* put the dysymbol_command in the buffer */
  393.         if(nsyms != 0){
  394.         memcpy(output_addr + offset, &dynamic_symbol_table,
  395.                sizeof(struct dysymtab_command));
  396.         if(host_byte_sex != md_target_byte_sex)
  397.             swap_dysymtab_command((struct dysymtab_command *)
  398.                       (output_addr + offset),
  399.                       md_target_byte_sex);
  400.         offset += sizeof(struct dysymtab_command);
  401.         }
  402.     }
  403.  
  404.     /* put the section contents (frags) in the buffer */
  405.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  406.         offset = frchainP->frch_section.offset;
  407.         for(fragP = frchainP->frch_root; fragP; fragP = fragP->fr_next){
  408.         know(fragP->fr_type == rs_fill);
  409.         /* put the fixed part of the frag in the buffer */
  410.         memcpy(output_addr + offset, fragP->fr_literal, fragP->fr_fix);
  411.         offset += fragP->fr_fix;
  412.  
  413.         /* put the variable repeated part of the frag in the buffer */
  414.         fill_literal = fragP->fr_literal + fragP->fr_fix;
  415.         fill_size = fragP->fr_var;
  416.         know(fragP->fr_offset >= 0);
  417.         for(count = fragP->fr_offset; count != 0;  count--){
  418.             memcpy(output_addr + offset, fill_literal, fill_size);
  419.             offset += fill_size;
  420.         }
  421.         }
  422.     }
  423.  
  424.  
  425.     /* put the symbols in the output file's buffer */
  426.     offset = symbol_table.symoff;
  427.     for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
  428.         if((symbolP->sy_type & N_EXT) == 0){
  429.         symbol_name = symbolP->sy_nlist.n_un.n_name;
  430.         symbolP->sy_nlist.n_un.n_strx = symbolP->sy_name_offset;
  431.         memcpy(output_addr + offset, (char *)(&symbolP->sy_nlist),
  432.                sizeof(struct nlist));
  433.         symbolP->sy_nlist.n_un.n_name = symbol_name;
  434.         offset += sizeof(struct nlist);
  435.         }
  436.     }
  437.     for(i = 0; i < nextdefsym; i++){
  438.         symbol_name = extdefsyms[i]->sy_nlist.n_un.n_name;
  439.         extdefsyms[i]->sy_nlist.n_un.n_strx = extdefsyms[i]->sy_name_offset;
  440.         memcpy(output_addr + offset, (char *)(&extdefsyms[i]->sy_nlist),
  441.                sizeof(struct nlist));
  442.         extdefsyms[i]->sy_nlist.n_un.n_name = symbol_name;
  443.         offset += sizeof(struct nlist);
  444.     }
  445.     for(j = 0; j < nundefsym; j++){
  446.         symbol_name = undefsyms[j]->sy_nlist.n_un.n_name;
  447.         undefsyms[j]->sy_nlist.n_un.n_strx = undefsyms[j]->sy_name_offset;
  448.         memcpy(output_addr + offset, (char *)(&undefsyms[j]->sy_nlist),
  449.                sizeof(struct nlist));
  450.         undefsyms[j]->sy_nlist.n_un.n_name = symbol_name;
  451.         offset += sizeof(struct nlist);
  452.     }
  453.     if(host_byte_sex != md_target_byte_sex)
  454.         swap_nlist((struct nlist *)(output_addr + symbol_table.symoff),
  455.                symbol_table.nsyms, md_target_byte_sex);
  456.  
  457.     /*
  458.      * Put the relocation entries for each section in the buffer.
  459.      */
  460.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  461.         offset = frchainP->frch_section.reloff;
  462.         for(fixP = frchainP->frch_fix_root; fixP; fixP = fixP->fx_next){
  463.         offset += fix_to_relocation_entries(
  464.                     fixP,
  465.                     frchainP->frch_section.addr,
  466.                     (struct relocation_info *)(output_addr +
  467.                                    offset));
  468.         }
  469.     }
  470.     if(host_byte_sex != md_target_byte_sex)
  471.         swap_relocation_info((struct relocation_info *)
  472.         (output_addr + reloff), nrelocs, md_target_byte_sex);
  473.  
  474.     if(flagseen['k']){
  475.         /* put the indirect symbol table in the buffer */
  476.         offset = dynamic_symbol_table.indirectsymoff;
  477.         for(frchainP = frchain_root;
  478.         frchainP != NULL;
  479.         frchainP = frchainP->frch_next){
  480.         section_type = frchainP->frch_section.flags & SECTION_TYPE;
  481.         if(section_type == S_NON_LAZY_SYMBOL_POINTERS ||
  482.            section_type == S_LAZY_SYMBOL_POINTERS ||
  483.            section_type == S_SYMBOL_STUBS){
  484.             /*
  485.              * For each indirect symbol put out the symbol number.
  486.              */
  487.             for(isymbolP = frchainP->frch_isym_root;
  488.             isymbolP != NULL;
  489.             isymbolP = isymbolP->isy_next){
  490.  
  491.             memcpy(output_addr + offset,
  492.                    (char *)(&isymbolP->isy_symbol->sy_number),
  493.                    sizeof(unsigned long));
  494.             offset += sizeof(unsigned long);
  495.             }
  496.         }
  497.         }
  498.         if(host_byte_sex != md_target_byte_sex){
  499.         indirect_symbols = (unsigned long *)(output_addr +
  500.                     dynamic_symbol_table.indirectsymoff);
  501.         swap_indirect_symbols(indirect_symbols, nindirectsyms, 
  502.                       md_target_byte_sex);
  503.         }
  504.     }
  505.  
  506.     /* put the strings in the output file's buffer */
  507.     offset = symbol_table.stroff;
  508.     if(symbol_table.strsize != 0){
  509.         zero = 0;
  510.         memcpy(output_addr + offset, (char *)&zero, sizeof(char));
  511.         offset += sizeof(char);
  512.     }
  513.     for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
  514.         /* Ordinary case: not .stabd. */
  515.         if(symbolP->sy_name != NULL){
  516.         if((symbolP->sy_type & N_EXT) != 0){
  517.             memcpy(output_addr + offset, symbolP->sy_name,
  518.                strlen(symbolP->sy_name) + 1);
  519.             offset += strlen(symbolP->sy_name) + 1;
  520.         }
  521.         }
  522.     }
  523.     for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
  524.         /* Ordinary case: not .stabd. */
  525.         if(symbolP->sy_name != NULL){
  526.         if((symbolP->sy_type & N_EXT) == 0){
  527.             memcpy(output_addr + offset, symbolP->sy_name,
  528.                strlen(symbolP->sy_name) + 1);
  529.             offset += strlen(symbolP->sy_name) + 1;
  530.         }
  531.         }
  532.     }
  533.     /*
  534.          * Create the output file.  The unlink() is done to handle the problem
  535.          * when the out_file_name is not writable but the directory allows the
  536.          * file to be removed (since the file may not be there the return code
  537.          * of the unlink() is ignored).
  538.          */
  539.     (void)unlink(out_file_name);
  540.     if((fd = open(out_file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1)
  541.         as_fatal("can't create output file: %s", out_file_name);
  542.     if(write(fd, output_addr, output_size) != output_size)
  543.         as_fatal("can't write output file");
  544.     if(close(fd) == -1)
  545.         as_fatal("can't close output file");
  546. }
  547.  
  548. /*
  549.  * layout_indirect_symbols() setups the indirect symbol tables by looking up or
  550.  * creating symbol from the indirect symbol names and recording the symbol
  551.  * pointers.  It returns the total count of indirect symbol table entries.
  552.  */
  553. static
  554. unsigned long
  555. layout_indirect_symbols(void)
  556. {
  557.     struct frchain *frchainP;
  558.     unsigned long section_type, total, count, stride;
  559.     isymbolS *isymbolP;
  560.     symbolS *symbolP;
  561.  
  562.     total = 0;
  563.     for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
  564.         section_type = frchainP->frch_section.flags & SECTION_TYPE;
  565.         if(section_type == S_NON_LAZY_SYMBOL_POINTERS ||
  566.            section_type == S_LAZY_SYMBOL_POINTERS ||
  567.            section_type == S_SYMBOL_STUBS){
  568.         /*
  569.          * For each indirect symbol name lookup or create a symbol
  570.          * table entry for it.  If this section is a lazy section or
  571.          * a stub and the symbol is undefined section mark the symbol
  572.          * as a lazy undefined reference.
  573.          */
  574.         count = 0;
  575.         for(isymbolP = frchainP->frch_isym_root;
  576.             isymbolP != NULL;
  577.             isymbolP = isymbolP->isy_next){
  578.  
  579.             symbolP = symbol_find_or_make(isymbolP->isy_name);
  580.             if((symbolP->sy_type & N_TYPE) == N_UNDF &&
  581.                (section_type == S_LAZY_SYMBOL_POINTERS ||
  582.                 section_type == S_SYMBOL_STUBS) )
  583.             symbolP->sy_desc |= REFERENCE_FLAG_UNDEFINED_LAZY;
  584.             isymbolP->isy_symbol = symbolP;
  585.             count++;
  586.         }
  587.         /*
  588.          * Check for missing indirect symbols.
  589.          */
  590.         if(section_type == S_SYMBOL_STUBS)
  591.             stride = frchainP->frch_section.reserved2;
  592.         else
  593.             stride = sizeof(unsigned long);
  594.         if(frchainP->frch_section.size / stride != count)
  595.             as_warn("missing indirect symbols for section (%s,%s)",
  596.                 frchainP->frch_section.segname,
  597.                 frchainP->frch_section.sectname);
  598.         /*
  599.          * Set the index into the indirect symbol table for this
  600.          * section into the reserved1 field.
  601.          */
  602.         frchainP->frch_section.reserved1 = total;
  603.         total += count;
  604.         }
  605.     }
  606.     return(total);
  607. }
  608.  
  609. /*
  610.  * layout_symbols() removes temporary symbols (symbols that are of the form L1
  611.  * and 1:) if the -L flag is not seen so the symbol table has only the symbols
  612.  * it will have in the output file.  Then each remaining symbol is given a
  613.  * symbol number and a string offset for the symbol name which also sizes the
  614.  * string table.
  615.  * The order of the symbol table is:
  616.  *    local symbols
  617.  *    defined external symbols (sorted by name)
  618.  *    undefined external symbols (sorted by name)
  619.  * The order of the string table is:
  620.  *    strings for external symbols
  621.  *    strings for local symbols
  622.  */
  623. static
  624. void
  625. layout_symbols(
  626. long *symbol_number,
  627. long *string_byte_count)
  628. {
  629.     unsigned long i, j;
  630.     symbolS *symbolP;
  631.     symbolS **symbolPP;
  632.     char *name;
  633.  
  634.     *symbol_number = 0;
  635.     *string_byte_count = sizeof(char);
  636.  
  637.     /*
  638.      * First pass through the symbols remove temporary symbols that are not
  639.      * going to be in the output file.  Also number the local symbols and
  640.      * assign string offset to external symbols.
  641.      */
  642.     symbolPP = &symbol_rootP;
  643.     while((symbolP = *symbolPP)){
  644.         name = symbolP->sy_name;
  645.         /*
  646.          * Deal with temporary symbols.  Temporary symbols start with 'L'
  647.          * but are not stabs.  It is an error if they are undefined.  They
  648.          * are removed if the -L flag is not seen else they are kept.
  649.          */
  650.         if(name != NULL &&
  651.            (symbolP->sy_nlist.n_type & N_STAB) == 0 &&
  652.            name[0] == 'L'){
  653.  
  654.             if((symbolP->sy_nlist.n_type & N_TYPE) == N_UNDF){
  655.             if(name[1] != '\0' && name[2] == '\001'){
  656.             as_bad("Undefined local symbol %c (%cf or %cb)",
  657.                 name[1], name[1], name[1]);
  658.             }
  659.             else{
  660.             as_bad("Undefined local symbol %s", name);
  661.             }
  662.             /* don't keep this symbol */
  663.             *symbolPP = symbolP->sy_next;
  664.         }
  665.             else if(flagseen['L'] || (symbolP->sy_type & N_EXT) != 0){
  666.             if((symbolP->sy_type & N_EXT) == 0){
  667.             nlocalsym++;
  668.             symbolP->sy_number = *symbol_number;
  669.             *symbol_number = *symbol_number + 1;
  670.             }
  671.             else{
  672.             nextdefsym++;
  673.             symbolP->sy_name_offset = *string_byte_count;
  674.             *string_byte_count += strlen(symbolP->sy_name) + 1;
  675.             }
  676.             symbolPP = &(symbolP->sy_next);
  677.         }
  678.         else{
  679.             /* don't keep this symbol */
  680.             *symbolPP = symbolP->sy_next;
  681.         }
  682.         }
  683.         /*
  684.          * All non-temporary symbols will be the symbol table in the output
  685.          * file.
  686.          */
  687.         else{
  688.         /* Any undefined symbols become N_EXT. */
  689.         if(symbolP->sy_type == N_UNDF)
  690.             symbolP->sy_type |= N_EXT;
  691.  
  692.         if((symbolP->sy_type & N_EXT) == 0){
  693.             symbolP->sy_number = *symbol_number;
  694.             *symbol_number = *symbol_number + 1;
  695.             nlocalsym++;
  696.         }
  697.         else{
  698.             if((symbolP->sy_type & N_TYPE) != N_UNDF)
  699.             nextdefsym++;
  700.             else
  701.             nundefsym++;
  702.  
  703.             if(name != NULL){
  704.             /* the ordinary case (symbol has a name) */
  705.             symbolP->sy_name_offset = *string_byte_count;
  706.             *string_byte_count += strlen(symbolP->sy_name) + 1;
  707.             }
  708.             else{
  709.             /* the .stabd case (symbol has no name) */
  710.             symbolP->sy_name_offset = 0;
  711.             }
  712.         }
  713.         symbolPP = &(symbolP->sy_next);
  714.         }
  715.     }
  716.  
  717.     /* Set the indexes for symbol groups into the symbol table */
  718.     ilocalsym = 0;
  719.     iextdefsym = nlocalsym;
  720.     iundefsym = nlocalsym + nextdefsym;
  721.  
  722.     /* allocate arrays for sorting externals by name */
  723.     extdefsyms = xmalloc(nextdefsym * sizeof(symbolS *));
  724.     undefsyms = xmalloc(nundefsym * sizeof(symbolS *));
  725.  
  726.     i = 0;
  727.     j = 0;
  728.     for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
  729.         if((symbolP->sy_type & N_EXT) == 0){
  730.         if(symbolP->sy_name != NULL){
  731.             /* the ordinary case (symbol has a name) */
  732.             symbolP->sy_name_offset = *string_byte_count;
  733.             *string_byte_count += strlen(symbolP->sy_name) + 1;
  734.         }
  735.         else{
  736.             /* the .stabd case (symbol has no name) */
  737.             symbolP->sy_name_offset = *string_byte_count;
  738.         }
  739.         }
  740.         else{
  741.         if((symbolP->sy_type & N_TYPE) != N_UNDF)
  742.             extdefsyms[i++] = symbolP;
  743.         else
  744.             undefsyms[j++] = symbolP;
  745.         }
  746.     }
  747.     qsort(extdefsyms, nextdefsym, sizeof(symbolS *),
  748.           (int (*)(const void *, const void *))qsort_compare);
  749.     qsort(undefsyms, nundefsym, sizeof(symbolS *),
  750.           (int (*)(const void *, const void *))qsort_compare);
  751.     for(i = 0; i < nextdefsym; i++){
  752.         extdefsyms[i]->sy_number = *symbol_number;
  753.         *symbol_number = *symbol_number + 1;
  754.     }
  755.     for(j = 0; j < nundefsym; j++){
  756.         undefsyms[j]->sy_number = *symbol_number;
  757.         *symbol_number = *symbol_number + 1;
  758.     }
  759. }
  760.  
  761. /*
  762.  * Function for qsort to sort symbol structs by their name
  763.  */
  764. static
  765. int
  766. qsort_compare(
  767. const symbolS **sym1,
  768. const symbolS **sym2)
  769. {
  770.         return(strcmp((*sym1)->sy_name, (*sym2)->sy_name));
  771. }
  772.  
  773. /*
  774.  * nrelocs_for_fix() returns the number of relocation entries needed for the
  775.  * specified fix structure.
  776.  */
  777. static
  778. unsigned long
  779. nrelocs_for_fix(
  780. struct fix *fixP)
  781. {
  782.     /*
  783.      * If fx_addsy is NULL then this fix needs no relocation entry.
  784.      */
  785.     if(fixP->fx_addsy == NULL)
  786.         return(0);
  787.  
  788.     /*
  789.      * If this fix has a subtract symbol it is a SECTDIFF relocation which
  790.      * takes two relocation entries.
  791.      */
  792.     if(fixP->fx_subsy != NULL)
  793.         return(2);
  794.  
  795.     /*
  796.      * For RISC machines whenever we have a relocation item using the half
  797.      * of an address a second a relocation item describing the other
  798.      * half of the address is used.
  799.      */
  800. #ifdef I860
  801.     if(fixP->fx_r_type == I860_RELOC_HIGH ||
  802.        fixP->fx_r_type == I860_RELOC_HIGHADJ)
  803.         return(2);
  804. #endif
  805. #ifdef M88K
  806.     if(fixP->fx_r_type == M88K_RELOC_HI16 ||
  807.        fixP->fx_r_type == M88K_RELOC_LO16)
  808.         return(2);
  809. #endif
  810. #ifdef M98K
  811.     if(fixP->fx_r_type == M98K_RELOC_HI16 ||
  812.        fixP->fx_r_type == M98K_RELOC_LO16 ||
  813.        fixP->fx_r_type == M98K_RELOC_HA16 ||
  814.        fixP->fx_r_type == M98K_RELOC_LO14)
  815.         return(2);
  816. #endif
  817. #ifdef HPPA
  818.     if(fixP->fx_r_type == HPPA_RELOC_HI21 ||
  819.        fixP->fx_r_type == HPPA_RELOC_LO14 ||
  820.        fixP->fx_r_type == HPPA_RELOC_BR17 ||
  821.        fixP->fx_r_type == HPPA_RELOC_JBSR)
  822.         return(2);
  823. #endif
  824. #ifdef SPARC
  825.     if(fixP->fx_r_type == SPARC_RELOC_HI22 ||
  826.        fixP->fx_r_type == SPARC_RELOC_LO10)
  827.         return(2);
  828. #endif
  829.     return(1);
  830. }
  831.  
  832. /*
  833.  * fix_to_relocation_entries() creates the needed relocation entries for the
  834.  * specified fix structure that is from a section who's address starts at
  835.  * sect_addr.  It returns the number of bytes of relocation_info structs it
  836.  * placed at riP.
  837.  */
  838. static
  839. unsigned long
  840. fix_to_relocation_entries(
  841. struct fix *fixP,
  842. unsigned long sect_addr,
  843. struct relocation_info *riP)
  844. {
  845.     struct symbol *symbolP;
  846.     unsigned long count;
  847.     struct scattered_relocation_info sri;
  848.     unsigned long sectdiff;
  849. #ifdef HPPA
  850.     unsigned long left21, right14;
  851. #endif
  852.  
  853.     /*
  854.      * If fx_addsy is NULL then this fix needs no relocation entry.
  855.      */
  856.     if(fixP->fx_addsy == NULL)
  857.         return(0);
  858.  
  859.     memset(riP, '\0', sizeof(struct relocation_info));
  860.     symbolP = fixP->fx_addsy;
  861.  
  862.     switch(fixP->fx_size){
  863.         case 1:
  864.         riP->r_length = 0;
  865.         break;
  866.         case 2:
  867.         riP->r_length = 1;
  868.         break;
  869.         case 4:
  870.         riP->r_length = 2;
  871.         break;
  872.         default:
  873.         as_fatal("Bad fx_size (0x%x) in fix_to_relocation_info()\n",
  874.              fixP->fx_size);
  875.     }
  876.     riP->r_pcrel = fixP->fx_pcrel;
  877.     riP->r_address = fixP->fx_frag->fr_address + fixP->fx_where -
  878.              sect_addr;
  879.     riP->r_type = fixP->fx_r_type;
  880.     /*
  881.      * For undefined symbols this will be an external relocation entry.
  882.      */
  883.     if((symbolP->sy_type & N_TYPE) == N_UNDF){
  884.         riP->r_extern = 1;
  885.         riP->r_symbolnum = symbolP->sy_number;
  886.     }
  887.     else{
  888.         /*
  889.          * For defined symbols this will be a local relocation entry
  890.          * (possibly a section difference or a scattered relocation entry).
  891.          */
  892.         riP->r_extern = 0;
  893.         riP->r_symbolnum = symbolP->sy_other; /* nsect */
  894.  
  895.         /*
  896.          * If we are allowed to use the new features that are incompatible
  897.          * with 3.2 determine if this is left as a local relocation entry or
  898.          * changed to a SECTDIFF relocation entry.  If this comes from a fix
  899.          * that has a subtract symbol it is a SECTDIFF relocation.  Which is
  900.          * "addsy - subsy + constant" where both symbols are defined in
  901.          * sections.  To encode all this information two scattered
  902.          * relocation entries are used.  The first has the add symbol value
  903.          * and the second has the subtract symbol value.
  904.          */
  905.         if(flagseen['k'] && fixP->fx_subsy != NULL){
  906. #ifdef HPPA
  907.         if(fixP->fx_r_type == HPPA_RELOC_HI21)
  908.             sectdiff = HPPA_RELOC_HI21_SECTDIFF;
  909.         else if(fixP->fx_r_type == HPPA_RELOC_LO14)
  910.             sectdiff = HPPA_RELOC_LO14_SECTDIFF;
  911.         else
  912. #endif
  913. #ifdef SPARC
  914.         if(fixP->fx_r_type == SPARC_RELOC_HI22)
  915.             sectdiff = SPARC_RELOC_HI22_SECTDIFF;
  916.         else if(fixP->fx_r_type == SPARC_RELOC_LO10)
  917.             sectdiff = SPARC_RELOC_LO10_SECTDIFF;
  918.         else
  919. #endif
  920.         {
  921.             if(fixP->fx_r_type != 0)
  922.             as_fatal("incorrect fx_r_type (%u) for fx_subsy != 0 "
  923.                  "in fix_to_relocation_info()",fixP->fx_r_type);
  924.             sectdiff = RELOC_SECTDIFF;
  925.         }
  926.         memset(&sri, '\0',sizeof(struct scattered_relocation_info));
  927.         sri.r_scattered = 1;
  928.         sri.r_length    = riP->r_length;
  929.         sri.r_pcrel     = riP->r_pcrel;
  930.         sri.r_address   = riP->r_address;
  931.         sri.r_type      = sectdiff;
  932.         sri.r_value     = symbolP->sy_value;
  933.         *riP = *((struct relocation_info *)&sri);
  934.         riP++;
  935.  
  936.         sri.r_type      = RELOC_PAIR;
  937.         sri.r_value     = fixP->fx_subsy->sy_value;
  938.         if(sectdiff == RELOC_SECTDIFF)
  939.             sri.r_address = 0;
  940. #ifdef HPPA
  941.         else if(sectdiff == HPPA_RELOC_HI21_SECTDIFF){
  942.             calc_hppa_HILO(symbolP->sy_value - fixP->fx_subsy->sy_value,
  943.                    fixP->fx_offset, &left21, &right14);
  944.             sri.r_address = right14 & 0x3fff;
  945.         }
  946.         else if(sectdiff == HPPA_RELOC_LO14_SECTDIFF){
  947.             calc_hppa_HILO(symbolP->sy_value - fixP->fx_subsy->sy_value,
  948.                    fixP->fx_offset, &left21, &right14);
  949.             sri.r_address = left21 >> 11;
  950.         }
  951. #endif
  952. #ifdef SPARC
  953.         else if(sectdiff == SPARC_RELOC_HI22_SECTDIFF){
  954.             sri.r_address = (symbolP->sy_value - fixP->fx_subsy->sy_value
  955.                      + fixP->fx_offset) & 0x3ff;
  956.         }
  957.         else if(sectdiff == SPARC_RELOC_LO10_SECTDIFF){
  958.             sri.r_address = ((symbolP->sy_value - fixP->fx_subsy->sy_value
  959.                      + fixP->fx_offset) >> 10) & 0x3fffff;
  960.         }
  961. #endif
  962.         *riP = *((struct relocation_info *)&sri);
  963.         return(2 * sizeof(struct relocation_info));
  964.         }
  965.         /*
  966.          * Determine if this is left as a local relocation entry must be
  967.          * changed to a scattered relocation entry.  These entries allow
  968.          * the link editor to scatter the contents of a section and a local
  969.          * relocation can't be used when an offset is added to the symbol's
  970.          * value (symbol + offset).  This is because the relocation must be
  971.          * based on the value of the symbol not the value of the expression.
  972.          * Thus a scattered relocation entry that encodes the address of the
  973.          * symbol is used when the offset is non-zero.
  974.          */
  975. #if !defined(I860)
  976.         /*
  977.          * For processors that don't have all references as unique 32 bits
  978.          * wide references scattered relocation entries are not generated.
  979.          * This is so that the link editor does not get stuck not being able
  980.          * to do the relocation if the high half of the reference is shared
  981.          * by two references to two different symbols.
  982.          */
  983.         if(fixP->fx_offset != 0 &&
  984.            ((symbolP->sy_type & N_TYPE) & ~N_EXT) != N_ABS
  985. #ifdef M68K
  986.            /*
  987.         * Since the m68k's pc relative branch instructions use the
  988.         * address of the beginning of the displacement (except for
  989.         * byte) the code in m68k.c when generating fixes adds to the
  990.         * offset 2 for word and 4 for long displacements.
  991.         */
  992.            && !(fixP->fx_pcrel &&
  993.                 ((fixP->fx_size == 2 && fixP->fx_offset == 2) ||
  994.                  (fixP->fx_size == 4 && fixP->fx_offset == 4)) )
  995. #endif /* M68K */
  996.            ){
  997.  
  998.         memset(&sri, '\0',sizeof(struct scattered_relocation_info));
  999.         sri.r_scattered = 1;
  1000.         sri.r_length    = riP->r_length;
  1001.         sri.r_pcrel     = riP->r_pcrel;
  1002.         sri.r_address   = riP->r_address;
  1003.         sri.r_type      = riP->r_type;
  1004.         sri.r_value     = symbolP->sy_value;
  1005.         *riP = *((struct relocation_info *)&sri);
  1006.         }
  1007. #endif /* !defined(I860) */
  1008.     }
  1009.     count = 1;
  1010.     riP++;
  1011.  
  1012. #if !defined(M68K) && !defined(I386)
  1013.     /*
  1014.      * For RISC machines whenever we have a relocation item using the half
  1015.      * of an address we also emit a relocation item describing the other
  1016.      * half of the address so the linker can reconstruct the address to do
  1017.      * the relocation.
  1018.      */
  1019. #ifdef I860
  1020.     if(fixP->fx_r_type == I860_RELOC_HIGH ||
  1021.        fixP->fx_r_type == I860_RELOC_HIGHADJ)
  1022. #endif
  1023. #ifdef M88K
  1024.     if(fixP->fx_r_type == M88K_RELOC_HI16 ||
  1025.        fixP->fx_r_type == M88K_RELOC_LO16)
  1026. #endif
  1027. #ifdef M98K
  1028.     if(fixP->fx_r_type == M98K_RELOC_HI16 ||
  1029.        fixP->fx_r_type == M98K_RELOC_LO16 ||
  1030.        fixP->fx_r_type == M98K_RELOC_HA16 ||
  1031.        fixP->fx_r_type == M98K_RELOC_LO14)
  1032. #endif
  1033. #ifdef HPPA
  1034.     if(fixP->fx_r_type == HPPA_RELOC_HI21 ||
  1035.        fixP->fx_r_type == HPPA_RELOC_LO14 ||
  1036.        fixP->fx_r_type == HPPA_RELOC_BR17 ||
  1037.        fixP->fx_r_type == HPPA_RELOC_JBSR)
  1038. #endif
  1039. #ifdef SPARC
  1040.     if(fixP->fx_r_type == SPARC_RELOC_HI22 ||
  1041.        fixP->fx_r_type == SPARC_RELOC_LO10)
  1042. #endif
  1043.     {
  1044.         memset(riP, '\0', sizeof(struct relocation_info));
  1045.         switch(fixP->fx_size){
  1046.         case 1:
  1047.             riP->r_length = 0;
  1048.             break;
  1049.         case 2:
  1050.             riP->r_length = 1;
  1051.             break;
  1052.         case 4:
  1053.             riP->r_length = 2;
  1054.             break;
  1055.         default:
  1056.             as_fatal("Bad fx_size (0x%x) in fix_to_relocation_info()\n",
  1057.                  fixP->fx_size);
  1058.         }
  1059.         riP->r_pcrel = fixP->fx_pcrel;
  1060.         /*
  1061.          * We set r_extern to 0, so other apps won't try to use r_symbolnum
  1062.          * as a symbol table indice.  We set all the bits of r_symbolnum so 
  1063.          * it is all but guaranteed to be outside the range we use for non-
  1064.          * external types to denote what section the relocation is in.
  1065.          */
  1066.         riP->r_extern = 0;
  1067.         riP->r_symbolnum = 0x00ffffff;
  1068. #ifdef I860
  1069.         riP->r_type    = I860_RELOC_PAIR;
  1070.         riP->r_address = 0xffff & fixP->fx_value;
  1071. #endif
  1072. #ifdef M88K
  1073.         riP->r_type = M88K_RELOC_PAIR;
  1074.         if(fixP->fx_r_type == M88K_RELOC_HI16)
  1075.         riP->r_address = 0xffff & fixP->fx_value;
  1076.         else if(fixP->fx_r_type == M88K_RELOC_LO16)
  1077.         riP->r_address = 0xffff & (fixP->fx_value >> 16);
  1078. #endif
  1079. #ifdef M98K
  1080.         riP->r_type = M98K_RELOC_PAIR;
  1081.         if(fixP->fx_r_type == M98K_RELOC_HI16 ||
  1082.            fixP->fx_r_type == M98K_RELOC_HA16)
  1083.         riP->r_address = 0xffff & fixP->fx_value;
  1084.         else if(fixP->fx_r_type == M98K_RELOC_LO16 ||
  1085.             fixP->fx_r_type == M98K_RELOC_LO14)
  1086.         riP->r_address = 0xffff & (fixP->fx_value >> 16);
  1087. #endif
  1088. #ifdef HPPA
  1089.         riP->r_type     = HPPA_RELOC_PAIR;
  1090.         calc_hppa_HILO(fixP->fx_value - fixP->fx_offset,
  1091.                fixP->fx_offset, &left21, &right14);
  1092.         if (fixP->fx_r_type == HPPA_RELOC_LO14 ||
  1093.         fixP->fx_r_type == HPPA_RELOC_BR17)
  1094.         riP->r_address = left21 >> 11;
  1095.         else if (fixP->fx_r_type == HPPA_RELOC_HI21)
  1096.         riP->r_address = right14 & 0x3fff;
  1097.         else if (fixP->fx_r_type == HPPA_RELOC_JBSR){
  1098.         if((symbolP->sy_type & N_TYPE) == N_UNDF)
  1099.             riP->r_address = fixP->fx_value & 0xffffff;
  1100.         else
  1101.             riP->r_address = (fixP->fx_value - sect_addr) & 0xffffff;
  1102.         }
  1103. #endif
  1104. #ifdef SPARC
  1105.         riP->r_type     = SPARC_RELOC_PAIR;
  1106.         if (fixP->fx_r_type == SPARC_RELOC_HI22)
  1107.         riP->r_address = fixP->fx_value & 0x3ff;
  1108.         else if (fixP->fx_r_type == SPARC_RELOC_LO10)
  1109.         riP->r_address = (fixP->fx_value >> 10) & 0x3fffff;
  1110. #endif
  1111.         count = 2;
  1112.     }
  1113. #endif /* !defined(M68K) && !defined(I386) */
  1114.     return(count * sizeof(struct relocation_info));
  1115. }
  1116.  
  1117. #ifdef I860
  1118. /*
  1119.  * set_default_section_align() is used to set a default minimum section
  1120.  * alignment if the section exist.
  1121.  */
  1122. static
  1123. void
  1124. set_default_section_align(
  1125. char *segname,
  1126. char *sectname,
  1127. unsigned long align)
  1128. {
  1129.     frchainS *frcP;
  1130.  
  1131.     for(frcP = frchain_root; frcP != NULL; frcP = frcP->frch_next){
  1132.         if(strncmp(frcP->frch_section.segname, segname,
  1133.                sizeof(frcP->frch_section.segname)) == 0 &&
  1134.            strncmp(frcP->frch_section.sectname, sectname,
  1135.                sizeof(frcP->frch_section.sectname)) == 0){
  1136.         if(align > frcP->frch_section.align)
  1137.             frcP->frch_section.align = align;
  1138.         return;
  1139.         }
  1140.     }
  1141. }
  1142.  
  1143. /* 
  1144.  * clear_section_flags() clears the section types for literals from the section
  1145.  * flags field.  This is needed for processors that don't have all references
  1146.  * to sections as unique 32 bits wide references.  In this case the literal
  1147.  * flags are not set.  This is so that the link editor does not merge them and
  1148.  * get stuck not being able to fit the relocated address in the item to be
  1149.  * relocated or if the high half of the reference is shared by two references
  1150.  * to different symbols (which can also stick the link editor).
  1151.  */
  1152. static
  1153. void
  1154. clear_section_flags(void)
  1155. {
  1156.     frchainS *frcP;
  1157.  
  1158.     for(frcP = frchain_root; frcP != NULL; frcP = frcP->frch_next)
  1159.         if(frcP->frch_section.flags != S_ZEROFILL)
  1160.         frcP->frch_section.flags = 0;
  1161. }
  1162.  
  1163. /*
  1164.  * I860_tweeks() preforms the tweeks needed by the I860 processor to get minimum
  1165.  * section alignments and no merging of literals by the link editor.
  1166.  */
  1167. static
  1168. void
  1169. I860_tweeks(void)
  1170. {
  1171.     set_default_section_align("__TEXT", "__text", 5);
  1172.     set_default_section_align("__DATA", "__data", 4);
  1173.     set_default_section_align("__DATA", "__bss",  4);
  1174.  
  1175.     clear_section_flags();
  1176. }
  1177. #endif
  1178.